package com.be.beadmin.service.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.service.dto.input.FileDataDto;
import com.be.beadmin.service.dto.output.FileDataEx;
import com.be.beadmin.service.entity.FileData;
import com.be.beadmin.service.impl.MinioService;
import com.be.beadmin.service.repository.FileDataRepository;
import com.be.beadmin.service.service.FileDataService;
import com.google.common.io.ByteStreams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
* @author chenwei
* @date 2023-02-20
**/
@RestController
public class FileDataController {

    @Autowired
    private  FileDataService fileDataService;

    @Autowired
    private FileDataRepository fileDataRepository;


    @Autowired
    private MinioService minioService;



    @PostMapping(value = "/system/getFileDataByPage")
    public ResultModel getFileDataByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(fileDataService.getFileDataByPage(pageEntity)) ;
    }

    @PostMapping("/system/addFileData")
    public ResultModel addFileData(@RequestBody FileDataDto fileDataDto){
        fileDataService.addFileData(fileDataDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/system/delFileData")
    public ResultModel delFileData(@RequestBody FileDataDto fileDataDto){
        fileDataService.delFileData(fileDataDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/system/updateFileData")
    public ResultModel updateFileData(@RequestBody FileDataDto fileDataDto){
        fileDataService.updateFileData(fileDataDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/system/exportFileData")
    public void exportFileData(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<FileDataEx> fileDataPage = fileDataService.getFileDataByPage(pageEntity);
        List<FileDataEx> fileDataExes = fileDataPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("FileData接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), FileDataEx.class).sheet("sheet1").doWrite(fileDataExes);
    }

    @PostMapping("/system/downloadFromMinio")
    public ResultModel getAfterSalesNeedInfoByPage(HttpServletResponse response, @RequestBody FileDataDto fileDataDto)  {

        try {
            FileData fileData = fileDataRepository.findFileDataById(fileDataDto.getId());
            InputStream inputStream = minioService.downloadOne(fileData.getBucketName(), fileData.getFileName());
            response.addHeader("Accept-Ranges", "bytes");
            response.addHeader("Content-disposition", "attachment;filename= 0505-0601print.pdf");
            response.addHeader("Content-Type", "text/plain;charset=utf-8");
            response.addHeader("Pragma","No-cache");
            response.addHeader("Cache-Control","No-cache");
            ServletOutputStream os = response.getOutputStream();

            ByteStreams.copy(inputStream, os);
            os.flush();
            return  ResultModel.success("下载成功");
        } catch (Exception e) {
            return ResultModel.error(99,"文件读取异常");
        }


    }


    @PostMapping("/system/uploadFromMinio")
    public ResultModel getAfterSalesNeedInfoByPage(@RequestParam MultipartFile file)  {

        try {
            minioService.uploadOne(file);
            return  ResultModel.success("上传成功!!");
        } catch (Exception e) {
            return ResultModel.error(99,e.getMessage());
        }
    }
}